package com.first.xiinn.dao.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.hibernate.Query;

import com.first.core.dao.impl.BaseDaoImpl;
import com.first.core.util.PagingBean;
import com.first.xiinn.dao.InMessageDao;
import com.first.xiinn.model.InMessage;
import com.first.xiinn.model.ShortMessage;

public class InMessageDaoImpl extends BaseDaoImpl<InMessage>
  implements InMessageDao
{
  public InMessageDaoImpl()
  {
    super(InMessage.class);
  }

  public InMessage findByRead(String userId)
  {
    String hql = "from InMessage vo where vo.readFlag=0 and vo.delFlag=0 and vo.userId=?";
    Object[] objs = { userId };
    List list = findByHql(hql, objs);
    if (list.size() > 0) {
      return (InMessage)list.get(list.size() - 1);
    }
    return null;
  }

  public Integer findByReadFlag(String userId)
  {
    String sql = "select count(*) from InMessage vo where vo.readFlag=0 and vo.delFlag=0 and vo.userId=" + userId;
    Query query = getSession().createQuery(sql);
    return Integer.valueOf(Integer.parseInt(query.list().iterator().next().toString()));
  }

  public List<InMessage> findAll(String userId, PagingBean pb)
  {
    String hql = "from InMessage vo where vo.userId=?";
    Object[] objs = { userId };
    return findByHql(hql, objs, pb);
  }

  public List<InMessage> findByShortMessage(ShortMessage shortMessage, PagingBean pb)
  {
    String hql = "from InMessage vo1,ShortMessage vo2 where vo1.shortMessage=?";
    Object[] objs = { shortMessage };
    return findByHql(hql, objs, pb);
  }

  public List findByUser(String userId, PagingBean pb)
  {
    String hql = "select vo1,vo2 from InMessage vo1,ShortMessage vo2 where vo1.shortMessage=vo2  and vo2.senderId=? order by vo2.sendTime desc";
    Object[] objs = { userId };
    return findByHql(hql, objs, pb);
  }

  public List findByUser(String userId)
  {
    String hql = "select vo1,vo2 from InMessage vo1,ShortMessage vo2 where vo1.shortMessage=vo2 and vo2.senderId=?";
    Object[] objs = { userId };
    return findByHql(hql, objs);
  }

  public List searchInMessage(String userId, InMessage inMessage, ShortMessage shortMessage, Date from, Date to, PagingBean pb)
  {
    StringBuffer hql = new StringBuffer("select vo1 ,vo2 from InMessage vo1,ShortMessage vo2 where vo1.shortMessage=vo2  and vo2.senderId=?");
    ArrayList paramList = new ArrayList();
    paramList.add(userId);
    if (to != null) {
      hql.append("and vo2.sendTime <= ?");
      paramList.add(to);
    }
    if (from != null) {
      hql.append("and vo2.sendTime >= ?");
      paramList.add(from);
    }
    if ((inMessage != null) && 
      (StringUtils.isNotEmpty(inMessage.getUserFullname()))) {
      hql.append(" and vo1.userFullname=?");
      paramList.add(inMessage.getUserFullname());
    }

    hql.append(" order by vo2.sendTime desc");

    return findByHql(hql.toString(), paramList.toArray(), pb);
  }

  public InMessage findLatest(String userId)
  {
    String hql = "from InMessage vo where vo.delFlag=0 and vo.userId=?";
    Object[] objs = { userId };
    List list = findByHql(hql, objs);
    if (list.size() > 0) {
      return (InMessage)list.get(list.size() - 1);
    }
    return null;
  }
}

